import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '../views/layout.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    // redirect:'',
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue'),
  },
  {
    path: '/',
    name: 'home',
    redirect:'list',
    component: Layout,
    children:[
      {
        path: 'list',
        name: 'list',
        component: () => import(/* webpackChunkName: "about" */ '../views/list.vue'),
      },
      {
        path: 'roles',
        name: 'roles',
        component: () => import(/* webpackChunkName: "about" */ '../views/roles.vue'),
      },
    ],
  },
  

 
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path=='/login') return next()
  else{
    const tokenStr=localStorage.getItem('token')
    if(!tokenStr){
      return next('/login')
    }
    next()
  }
})
export default router
